package it.yuzuojian.com.controller;

import it.yuzuojian.com.service.UserService;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/login.html")
    public String index(){
        return "admin/login";
    }
    @PostMapping("/login")
    public String login(String name, String password, Model model){
        try {
            userService.login(name, password);
        } catch (UnknownAccountException e) {
            model.addAttribute("msg", "不存在该用户");
            return "admin/login";
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", "密码错误");
            return "admin/login";
        }
        return "admin/index";
    }
    @GetMapping("/logout")
    public String logout(ModelMap modelMap){

        return "redirect:/login.html";
    }
}
